package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;


public class DBUtil { // 连接/关闭数据库
	private static final String CLASSNAME = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@ZHA-ITA092-W7:1522:orcl";
	private static final String USERNAME = "lionel";
	private static final String PSD = "123456";
	private static BasicDataSource ds;
	static {
		ds = new BasicDataSource();
		ds.setUrl(URL);
		ds.setDriverClassName(CLASSNAME);
		ds.setUsername(USERNAME);
		ds.setPassword(PSD);
	}
	
	public static Connection createConnectionWitDatasource() {
		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}

	public static Connection creatConnection() {
		Connection con = null;
		try {
			Class.forName(CLASSNAME);
			con = DriverManager.getConnection(URL, USERNAME, PSD);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return con;

	}

	public static void close(Connection con, PreparedStatement pst, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (pst != null) {
				pst.close();
			}
			if (con != null) {
				con.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
